// https://blog.csdn.net/u010228448/article/details/59520238
// Created by Joey on 2019/12/17.
//


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include <fstream>

using namespace std;
#define LongOfChar 100  // 定义了一个常量为100的宏LongOfChar

/**
* The class of Player
* 2017/2/19
*/
class Player {
private:
    int dan;      //棋手段位
public :
    char name[LongOfChar]; //棋手姓名
    int mikeScore;
public:
    Player() {
        dan = 0;
        mikeScore = 0;
    }

    void setDan(int da) {
        dan = da;
    }

    int getDan() {
        return dan;
    }
};

/*Player降序排列*/
class CompGreater {
public:
    bool operator()(const Player &pstItem1, const Player &pstItem2) {
        return pstItem1.mikeScore > pstItem2.mikeScore;
    }

};


/*
* The Class Of PlayerPair
* 2017/2/23
*/
class PlayerPair {
public :
    Player *A_Player;  //对手中A的指针
    Player *B_Player;  //对手中B的指针
    Player *winer_Player; //获胜者的指针
};


/**
* The class of Complete
* 2017/2/19
*/
class WeiQiComplete {
public :
    int numberOfPlayer;     //参赛人数
    int roundOfCompelte;    //比赛轮数
    int currentCompelte;    //当前第几轮
    vector<Player> mPlayerVetor;   //存放参赛选手的Vector
    vector<PlayerPair> mPlayerPairVetor; //存放配对实体的Vector
public :
    WeiQiComplete() {
        numberOfPlayer = 0;
        roundOfCompelte = 0;
        currentCompelte = 0;
    }

    //Print the info of player
    void printPlayer() {   //循环打印所有参赛选手信息
        vector<Player>::iterator it;
        cout << "The In of Player:" << endl;
        for (it = mPlayerVetor.begin(); it != mPlayerVetor.end(); it++) {
            cout << "name:" << (*it).name << "  dan:" << (*it).getDan() << "  MikeScore:" << (*it).mikeScore << endl;
        }
    }

    //input the info of complete
    void inputPlayer() { //输入比赛信息（根据当前第几轮判断是否已经开始比赛，开始则不可以输入）
        if (currentCompelte > 0) {
            cout << "the complete has begin,you cannot enter again!";
        } else {
            cout << "Please input the number of player :" << endl;
            cin >> numberOfPlayer;
            cout << "Please input the round of Compelte :" << endl;
            cin >> roundOfCompelte;
            cout << "Please input the Info of all Player" << endl;
            for (int i = 0; i < numberOfPlayer; i++) {
                Player mPlayer;
                int dan;
                cout << "Please input name of " << i + 1 << " player :" << endl;
                cin >> mPlayer.name;
                cout << "Please input dan of " << i + 1 << " player :" << endl;
                cin >> dan;
                mPlayer.setDan(dan);
                mPlayer.mikeScore = dan;
                mPlayerVetor.push_back(mPlayer);
            }
        }


    }

    //sort the Player
    void sortPlayer() {  //根据选手的麦克马洪分数对选手进行排序
        sort(mPlayerVetor.begin(), mPlayerVetor.end(), CompGreater());
    }

    //make Pair of Player //根据选手的麦克马洪分数从高到低依次配对
    void makePair() {
        int sizeOfArray = mPlayerVetor.size() / 2;
        for (int i = 0; i < sizeOfArray; i++) {
            PlayerPair mPlayerPair;
            mPlayerPair.A_Player = &mPlayerVetor[2 * i];
            mPlayerPair.B_Player = &mPlayerVetor[2 * i + 1];
            mPlayerPairVetor.push_back(mPlayerPair);
        }
        if (sizeOfArray * 2 < mPlayerVetor.size()) {
            mPlayerVetor[mPlayerVetor.size() - 1].mikeScore += 0.5;
        }
    }

    //print the pair of Player
    void printpair() {  //打印出所有的分组信息
        cout << "*The Result of the pair*" << endl;
        for (int i = 0; i < mPlayerPairVetor.size(); i++) {
            cout << (*(mPlayerPairVetor[i].A_Player)).name << "   :   " << (*(mPlayerPairVetor[i].B_Player)).name
                 << endl;
        }
        cout << "you can enter the result " << endl;
    }

    //print the Result of round
    void printTheResult() { //打印此轮的比赛结果
        for (int i = 0; i < mPlayerPairVetor.size(); i++) {
            cout << (*(mPlayerPairVetor[i].A_Player)).name << "  :  " << (*(mPlayerPairVetor[i].B_Player)).name
                 << "  Winer is *" << (*(mPlayerPairVetor[i].winer_Player)).name << "*" << endl;
        }
        if (roundOfCompelte == currentCompelte) {
            cout << "The Complete End ! The Sort Of all Payer is --" << endl;
            sortPlayer();
            printPlayer();
        }
    }

    //input the result of round
    void inputResult() {  //输入此轮的比赛每组的结果
        for (int i = 0; i < mPlayerPairVetor.size(); i++) {
            int playerid;
            cout << "please input the result of " << (*(mPlayerPairVetor[i].A_Player)).name << " win is 1 ; "
                 << (*(mPlayerPairVetor[i].B_Player)).name << " win is 2 " << endl;
            cin >> playerid;
            if (playerid == 1) {
                mPlayerPairVetor[i].winer_Player = mPlayerPairVetor[i].A_Player;
                (*(mPlayerPairVetor[i].A_Player)).mikeScore += 1;
            } else {
                if (playerid == 2) {
                    mPlayerPairVetor[i].winer_Player = mPlayerPairVetor[i].B_Player;
                    (*(mPlayerPairVetor[i].B_Player)).mikeScore += 1;
                } else {
                    (*(mPlayerPairVetor[i].A_Player)).mikeScore += 0.5;
                    (*(mPlayerPairVetor[i].B_Player)).mikeScore += 0.5;
                }
            }
        }
    }
};


/**
* The class 0f Login
* 2017/2/26
*/
class Login {
private :
    string name;
    string password;
public:
    void login() {  //登录验证的入口
        cout << "*************************************" << endl;
        cout << "           WeiQi System              " << endl;
        cout << "*************************************" << endl;
        cout << "Please input your name and password :" << endl;
        input();
        if (login_Verification(name, password)) {
            login_success();
        } else {
            login_error();
        }
    }

    //input info of login
    void input() {  //输入登录信息
        cout << "Your name:";
        cin >> name;
        cout << "Your password:";
        cin >> password;
    }

    //Verification
    bool login_Verification(string name, string password) { //比对登录信息
        bool resp = false;
        if (name.compare("li") == 0 && password.compare("123") == 0) {
            resp = true;
        }
        return resp;
    }

    //login success
    void login_success() {  //比对成功后
        cout << "Login Success !" << endl;
    }

    //login error
    void login_error() {   //比对失败后
        cout << "Your name Or Password is Error!" << endl;
        login();
    }
};

class fileUtil {
public:
    static void outPutToFile(WeiQiComplete weiQiComplete) {   //将类对象打印保存到文件中
        ofstream file;
        file.open("WeiQiComplete.txt", ios::out | ios::binary | ios::app);
        file.write((char *) &weiQiComplete, sizeof(WeiQiComplete));
        file.close();
    }

    static WeiQiComplete readFromFile() { //从文件中将之前所保存的对象读取出来
        WeiQiComplete weiQiComplete;
        ifstream infile;
        infile.open("WeiQiComplete.txt", ios::in | ios::binary);
        if (infile.is_open()) {
            infile.read((char *) &weiQiComplete, sizeof(weiQiComplete));     //(char *)&weiQiComplete 指读存放读取到的数据的对象的指针
            infile.close();                                               //sizeof(weiQiComplete) 存放读取数据所需要的空间大小
            cout << "finish read file " << endl;
        }
        print(weiQiComplete);
        return weiQiComplete;
    }

    static void print(WeiQiComplete weiQiComplete) {
        cout << " have read a  WeiQiComplete Info " << ":" << &weiQiComplete << endl;
    }
};

/**
* The class 0f Menu
* 2017/2/27
*/
class Menu {   //菜单类
    WeiQiComplete mWeiQiComplete;
public:
    void menu() {
        int select;
        cout << "******************************************" << endl;
        cout << "              WelCome You                 " << endl;
        cout << "This is Menu ,you can input the number of list" << endl;
        cout << "* 1: Enter The Number of Player And Round " << endl;
        cout << "* 2: Start a Round " << endl;
        cout << "* 3: Enter The Result Of Round" << endl;
        cout << "* 4: Print The Sort Of Player" << endl;
        cout << "* 5: Save to the file" << endl;
        cout << "* 6: read  the file" << endl;
        cout << "* 7: Back " << endl;
        cout << "******************************************" << endl;

        cin >> select;
        switch (select) {
            case 1:
                enterInfo();
                break;
            case 2:
                startRound();
                break;
            case 3:
                inputResultOfRound();
                break;
            case 4:
                printSort();
                break;
            case 5:
                SaveToFile();
                break;
            case 6:
                ReadFromFile();
                break;
            case 7:
                exit(0);
                break;
        }
    }

    void enterInfo() { //输入比赛信息
        mWeiQiComplete.inputPlayer();
        mWeiQiComplete.printPlayer();
        mWeiQiComplete.sortPlayer();

        menu();
    }

    void startRound() {  //开始新的一轮比赛
        if (mWeiQiComplete.numberOfPlayer <= 0 || mWeiQiComplete.roundOfCompelte <= 0) {
            cout << "Please Enter the Info About Player and Compelte!" << endl;
            menu();
        } else {
            if (mWeiQiComplete.roundOfCompelte <= mWeiQiComplete.currentCompelte) {
                cout << "The Compelte Has Finish！" << endl;
                menu();
            } else {
                mWeiQiComplete.currentCompelte = mWeiQiComplete.currentCompelte + 1;
                cout << "you will begin the  * " << mWeiQiComplete.currentCompelte << "  * round " << endl;
                mWeiQiComplete.makePair();
                mWeiQiComplete.printpair();
                menu();
            }
        }
    }

    void printSort() { //输出排序后的选手信息
        mWeiQiComplete.printPlayer();
        menu();
    }

    void inputResultOfRound() { //输入每轮比赛每组结果
        mWeiQiComplete.inputResult();
        mWeiQiComplete.printTheResult();
        menu();
    }

    void SaveToFile() { //将比赛对象整体保存到文件中
        fileUtil::outPutToFile(mWeiQiComplete);
        menu();
    }

    void ReadFromFile() { //从已存在的文件读取比赛对象数据
        mWeiQiComplete = fileUtil::readFromFile();
        menu();
    }
};

/**
* main method
* 2017/2/19
*/
int main() {
    Menu mMenu;
    Login mLogin;
    mLogin.login();
    mMenu.menu();
    return 0;
}
